// basicnpc.txt
// A very simple, naive text. Creature attacks anything it hates nearby.
// Once it has won, it returns to its home.
// Memory Cells:
//   Cell 0 - If 0, default behavior. If 1, it doesnt fidget.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.
//   Cell 3,4,5 - The three creations it can summon. GIVE THE IN-GAME NUMBER
//   Cell 6 - Talking node

begincreaturescript;

variables;

short i,target;
short last_spawn;
short r1;
short last_abil;
short used_abil;
short call = 0;

body;

beginstate INIT_STATE;
	last_spawn = get_current_tick();
	last_abil = get_current_tick();
	
	set_name(ME,"Sage Taygen");
	set_level(ME,30);
	change_max_health(ME,500);
	set_boss_level(ME,2);
	set_char_unkillable(ME,1);
	if (gf(64,10) > 0)
		erase_char(ME);
		
	set_resistance(ME,7,90);

	if (gf(100,16) > 0)
		erase_char(ME);
	break;

beginstate DEAD_STATE;

break;

beginstate START_STATE; 
	if (gf(64,10) > 0)
		set_state(4);
		
	// if I have a target for some reason, go attack it
	if (target_ok()) {
			set_state(3);
		}
	
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	if (my_dist_from_start() >= 6) {
		if (get_ran(1,1,100) < 40) 
			return_to_start(ME,1);
		}
		else if (get_memory_cell(0) == 0)
			fidget(ME,25);

	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (gf(64,10) > 0)
		set_state(4);

	if (target_ok() == FALSE)
		set_state(START_STATE);

	if ((gf(63,22) == 0) && (get_attitude(ME) >= 10)) {
		sf(63,22,1);
		begin_talk_mode(108);
		}
	if ((get_health(ME) < get_max_health(ME) / 2) || (gf(63,21) > 3)) {
		sf(64,10,1);
		begin_talk_mode(109);
		}
	
	if ((call == 0) && (get_health(ME) < (get_max_health(ME) * 7) / 8)) {
		call = 1;
		print_named_str(ME,"shouts for help from his guards.");
		alert_char(43);
		alert_char(44);
		alert_char(45);
		}
		
	if ((tick_difference(last_spawn,get_current_tick()) > 1) && (get_attitude(ME) >= 10) && 
	  (get_nearest_party_char(8) >= 0)) {
		r1 = get_ran(1,0,2);
		if ((get_memory_cell(3 + r1) > 0) && (char_ok(get_memory_cell(3 + r1)) == FALSE)) {
			if (summon_creature(get_memory_cell(3 + r1) - 8)) {
				print_named_str(ME,"makes a new creation!");
				set_summon_level(get_memory_cell(3 + r1),1);
				place_particle_num(get_memory_cell(3 + r1),10,7,10);
				
				if (get_level(get_memory_cell(3 + r1)) < get_level(ME) - 4)
					set_attack_bonus(get_memory_cell(3 + r1),get_level(ME) - get_level(get_memory_cell(3 + r1)) - 4);
				
				run_char_animation(2,1,50);	
				pc_heard_sound_delay(136,100);						
				
				last_spawn = get_current_tick();
				end();
				}
			}
		}

	used_abil = 0;
	if ( (tick_difference(last_abil,get_current_tick()) > 0) && (is_combat()) && (get_nearest_party_char(8) >= 0)) {
		if ((get_char_status(ME,1) <= 0) && (get_ran(1,0,100) < 40)) {
			run_char_animation(2,1,35);	
			print_named_str(ME,"starts to move faster!");
			pc_heard_sound_delay(101,250);						
		  	place_particle_num(ME,3,0,8);
			set_char_status(ME,1,5);
			last_abil = get_current_tick();
			used_abil = 1;
			}
			else if ((get_char_status(ME,8) <= 0) && (get_ran(1,0,100) < 20)) {
				run_char_animation(2,1,35);	
				print_named_str(ME,"summons a shield of power.");
				pc_heard_sound_delay(101,250);						
		 	 	place_particle_num(ME,1,0,8);
				set_char_status(ME,8,6);
				set_char_status(ME,0,6);
				set_char_status(ME,16,6);
				last_abil = get_current_tick();
				used_abil = 1;
				}

		}

	if ((is_combat()) && 
	  (tick_difference(last_abil,get_current_tick()) > 1)) {
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();

		  	place_particle_num(ME,1,4,8);
			print_named_str(ME,"surrounds himself with an aura of lightning.");
			pc_heard_sound_delay(179,250);						
	  		create_missile_spiral(147,40,8,2);
	  		damage_nearby(get_ran(1,200,300),8,1,0);
			status_nearby(get_level(ME) / 4,8,20,0);

		end();
		}


	if (used_abil)
		end();
		
	do_attack();
break;


beginstate 4; // fleeing state
	 set_act_at_dist(ME,1);

	if ((is_combat()) && 
	  (tick_difference(last_abil,get_current_tick()) > 1)) {
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();

			print_named_str(ME,"casts a powerful fear spell.");
		  	place_particle_num(ME,0,6,8);
			pc_heard_sound_delay(100,250);						
			status_nearby(100,7,7,0);
		
		if ((char_ok(9)) && (get_attitude(9) < 10) && (get_char_status(9,7) == 0)) {
			if (can_see_char(9))
				force_char_status(9,7,3);
			}
		if ((char_ok(10)) && (get_attitude(10) < 10) && (get_char_status(10,7) == 0)) {
			if (can_see_char(10))
				force_char_status(10,7,3);
			}
		if ((char_ok(11)) && (get_attitude(11) < 10) && (get_char_status(11,7) == 0)) {
			if (can_see_char(11))
				force_char_status(11,7,3);
			}
		if ((char_ok(12)) && (get_attitude(12) < 10) && (get_char_status(12,7) == 0)) {
			if (can_see_char(12))
				force_char_status(12,7,3);
			}
		if ((char_ok(13)) && (get_attitude(13) < 10) && (get_char_status(13,7) == 0)) {
			if (can_see_char(13))
				force_char_status(13,7,3);
			}
		if ((char_ok(14)) && (get_attitude(14) < 10) && (get_char_status(14,7) == 0)) {
			if (can_see_char(14))
				force_char_status(14,7,3);
			}
			
		end();
		}
		
	if (dist_to_nav_point(ME,2) <= 2) {
		print_named_str(ME,"escapes from the Zephyr Oasis.");
		erase_char(ME);
		}
	approach_nav_point(ME,2,1);

break;

beginstate TALKING_STATE;
	if (gf(100,4) > 2)
		begin_talk_mode(119);
		else if (gf(100,21) > 0)
			begin_talk_mode(110);
			else begin_talk_mode(30);

break;